package com.jsgt.greekfire.ui.home.entrance.createwallet.checkhelpword.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import com.fhx.core.base.BaseAdapter
import com.fhx.core.base.BaseViewHolder
import com.jsgt.greekfire.R

/**
 * 验证助记词列表适配器
 */
class WordCheckAdapter : BaseAdapter<String>() {

    inner class VH(itemView: View) : BaseViewHolder(itemView) {
        var deleteIv: ImageView? = null
        private var wordTv: TextView? = null

        init {
            deleteIv = itemView.findViewById(R.id.word_delete_iv)
            wordTv = itemView.findViewById(R.id.word_tv)
        }

        fun bindData(word: String) {
            wordTv?.text = word
            deleteIv?.setOnClickListener {
                val position = it.tag as Int
                dataList().removeAt(position)
                notifyDataSetChanged()
                viewClickListener?.invoke(word)
            }
        }
    }

    override fun provideViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder? {
        val view =
            LayoutInflater.from(parent.context).inflate(R.layout.item_word_check, parent, false)
        return VH(view)
    }

    override fun onBindViewHolder(holder: BaseViewHolder, position: Int) {
        val viewHolder = holder as VH
        viewHolder.bindData(dataList()[position])
        viewHolder.deleteIv?.tag = position
        viewHolder.deleteIv?.visibility = if (position == itemCount - 1) {
            View.VISIBLE
        } else {
            View.INVISIBLE
        }
    }
}